package ysq.xyg.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.model.Banner;
import ysq.xyg.model.info.BannerInfo;
import ysq.xyg.model.info.Page;
import ysq.xyg.model.info.ReturnInfo;
import ysq.xyg.service.BannerService;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.BaseUtil;
import ysq.xyg.util.DateUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.Date;

/**
 * 广告图片
 *
 * @author congcong tao
 * @create 2018/8/7 下午8:23
 */
@Controller
@RequestMapping("/banner")
public class BannerController extends BaseController {

    private static Logger log= LoggerFactory.getLogger(BannerController.class);
    @Resource
    private BannerService bannerService;

    private Banner banner;

    @RequestMapping("/listPage")
    public ModelAndView List(HttpServletRequest request, HttpServletResponse response,String menuid){
        return new ModelAndView("/bannerList");
    }

    @RequestMapping(value = "/jsonList",method = RequestMethod.POST)
    public @ResponseBody JSONObject jsonList(Banner banner, Page page) throws MalformedURLException, UnsupportedEncodingException {
        PageInfo<Banner> pageInfo = bannerService.pageInfo(banner, page.getLimit(), page.getPage());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("count", pageInfo.getTotal());
        if (pageInfo != null && pageInfo.getList() != null && pageInfo.getList().size() > 0) {
            JSONObject row = null;
            JSONArray rows = new JSONArray();
            for (Banner item : pageInfo.getList()) {
                row=new JSONObject();
                row.put("id",item.getId());
                row.put("image",item.getImage());
                row.put("title",item.getTitle());
                row.put("type",item.getType());
                row.put("orderNum",item.getOrderNum());
                row.put("status",item.getStatus());
                row.put("createTime", DateUtil.DateToStr(item.getCreateTime(), DateUtil.DATE_TIME_PATTERN));
                rows.add(row);
            }
            jsonObject.put("data",rows);
        }
        return jsonObject;
    }

    @RequestMapping(value = "/selectEntity",method = RequestMethod.GET)
    public ModelAndView selectEntity(String id){
        ModelAndView view = new ModelAndView("/bannerUpdateOrSave");
        banner = bannerService.selectByPrimaryKey(id);
        view.addObject("banner", banner);
        return view;
    }

    @RequestMapping(value = "/updateOrAddEntity",method = RequestMethod.POST)
    public @ResponseBody JSONObject updateOrAddEntity(BannerInfo bannerInfo) {
        ReturnInfo returnInfo = new ReturnInfo();
        JSONObject jsonObject = new JSONObject();
        if (StringUtil.isEmpty(bannerInfo.getId())) {
            banner = new Banner();
            banner.setId(BaseUtil.uId());
            banner.setType(bannerInfo.getType());
            banner.setImage(bannerInfo.getImage());
            banner.setTitle(bannerInfo.getTitle());
            banner.setStatus(bannerInfo.getStatus());
            banner.setOrderNum(bannerInfo.getOrderNum());
            banner.setUrl(bannerInfo.getUrl());
            banner.setCreateTime(new Date());
            int id = bannerService.insert(banner);
            if(id <= 0){
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_INSERT);
            }
        } else {
            banner = bannerService.selectByPrimaryKey(bannerInfo.getId());
            banner.setType(bannerInfo.getType());
            banner.setImage(bannerInfo.getImage());
            banner.setTitle(bannerInfo.getTitle());
            banner.setStatus(bannerInfo.getStatus());
            banner.setUrl(bannerInfo.getUrl());
            banner.setOrderNum(bannerInfo.getOrderNum());
            int id = bannerService.updateByPrimaryKey(banner);
            if (id <= 0) {
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
        jsonObject.put("returnInfo", returnInfo);
        return jsonObject;
    }

    @RequestMapping(value = "/deleteEntity",method = RequestMethod.GET)
    public @ResponseBody JSONObject deleteEntity(String id){
        ReturnInfo returnInfo=new ReturnInfo();
        JSONObject jsonObject=new JSONObject();
        if(StringUtil.isEmpty(id)){
            returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_CANSHU);
        }else{
            int ids = bannerService.deleteByPrimaryKey(id);
            if(ids <= 0){
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_DELETE);
            }
        }
        jsonObject.put("returnInfo",returnInfo);
        return jsonObject;
    }
}
